﻿// toast
var selectdate = '';
var yaodian;
$(function () {
    var user = getCookie('user');
    var org = getCookie('org');
    yaodian = getCookie('orgid');
    if (user == undefined || user == null || user == '') {
        //alert("请重新登陆！");
        $.alert("请重新登陆！", "未登录", function () {
            //return alert('回调');
            window.location.href = '../login.html';
        });
    } else {
        //alert('当前用户：' + user);
        $('#dqyd').text('当前门店：' + org);
    }

    //
    var qureyShenFenZheng = getQuery('ShenFenZheng');
    if (qureyShenFenZheng != undefined && qureyShenFenZheng != null && qureyShenFenZheng != '') {
        $('#js_input_sfz').val(qureyShenFenZheng);
    }
    var qureyTelephone = getQuery('telephone');
    if (qureyTelephone != undefined && qureyTelephone != null && qureyTelephone != '') {
        $('#js_input_dhhm').val(qureyTelephone);
    }
    //

    var $input = $('#js_input_xm');
    var $input_dhhm = $('#js_input_dhhm');

    $input.on('input', function () {
        if ($input.val()) {
            $('#showTooltips').removeClass('weui-btn_disabled');
        } else {
            $('#showTooltips').addClass('weui-btn_disabled');
        }
    });

    $input_dhhm.on('input', function () {
        if ($input_dhhm.val().length > 11) {
            $input_dhhm.val($input_dhhm.val().slice(0, 11));
        }
    });

    //保存预定信息
    $('#showTooltips').on('click', function () {

        SendOrder();



    });

    //设置最大的购买数目
    getKouZhaoPram();
});


function getKouZhaoPram() {
    $.ajax({
        type: "GET",
        url: "http://" + B_Url + "/api/baseInfo?info=kouzhaoshu",
        //数据，json字符串
        //data: JSON.stringify(list),
        success: function (result) {
            //设置最大的购买数目
            setMax_Kouzhao(result.ParamValue, 1);
        },
        error: function (e) { }
    });

}


function setMax_Kouzhao(MAX = 3, MIN = 1) {
    //var MAX = 3, MIN = 1;
    $('#js_input_sl').val(MAX);
    $('.weui-count__decrease').click(function (e) {
        var $input = $(e.currentTarget).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") - 1;
        if (number < MIN) number = MIN;
        $input.val(number);
    });
    $('.weui-count__increase').click(function (e) {
        var $input = $(e.currentTarget).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") + 1;
        if (number > MAX) number = MAX;
        $input.val(number);
    });
}


//发送当前预定信息
function SendOrder() {

    //var yaodian = $('#job').val();
    var xm = $('#js_input_xm').val();
    var sfz = $('#js_input_sfz').val();
    var jtzz = '';//$('#js_input_jtzz').val();
    var wx = '';
    try {
        var fingerprint1 = new Fingerprint({ screen_resolution: true }).get();
        wx = fingerprint1;
    }
    catch (err) { }

    var sl = $('#js_input_sl').val();
    var dhhm = $('#js_input_dhhm').val();

    if (yaodian == undefined || yaodian == null || yaodian == '') {
        $.alert("请选择药店！", "信息不全", function () {
        });
        return;
    }
    if (xm == undefined || xm == null || xm == '') {
        $.alert("请输入姓名！", "信息不全", function () {
        });
        return;
    }
    if (sfz == undefined || sfz == null || sfz == '') {
        $.alert("请输入身份证！", "信息不全", function () {
        });
        return;
    }
    //if (jtzz == undefined || jtzz == null || jtzz == '') {
    //    $.alert("请输入家庭住址！", "信息不全", function () {
    //    });
    //    return;
    //}
    if (sl == undefined || sl == null || sl == 0) {
        $.alert("数量不正确！", "信息不全", function () {
        });
        return;
    }
    if (dhhm == undefined || dhhm == null || dhhm == '') {
        $.alert("请输入电话号码！", "信息不全", function () {
        });
        return;
    }

    if (!checkPhone(dhhm)) {
        $.alert("手机号证校验失败！", "信息不全", function () {
        });
        return;
    }

    if (!checkIdCard(sfz)) {
        $.alert("身份证校验失败！", "信息不全", function () {
        });
        return;
    }

    $.showLoading();
    var tm = setTimeout(function () {
        $.hideLoading();
        $.alert("系统繁忙，请稍后再试！", "超时", function () { });
    }, 20000);

    var date = new Date();
    selectdate = getDateNumber(date);

    $.ajax({
        type: "GET",
        url: "http://" + B_Url + "/api/yudingsave",
        //数据，json字符串
        data: { info: 'saveorder', online: '1', yaodian: yaodian, xm: xm, sfz: sfz, jtzz: jtzz, sl: sl, dhhm: dhhm, wx: wx, selectdate: selectdate},
        success: function (result) {
            $.hideLoading();
            clearTimeout(tm);
            if (result == undefined || result == null || result == -1) {
                $.alert("", "购买失败", function () {
                    return;
                });
            } else {
                var msg = "购买成功！";
                if (result == -5) {
                    msg = '不能重复购买！';
                } else if (result == -2) {
                    msg = '库存不足！';
                } else if (result == -999) {
                    msg = '购买出错！';
                }
                $.alert("", msg, function () {
                    if (result > 0) {
                        window.location.href = "http://" + B_Url + '/Main/index.html';
                    }
                });
            }
        },
        error: function (e) { }
    });

}